package org.jsets.rpc.transfer;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.jsets.rpc.conf.SerivceConfig;
import org.jsets.rpc.processor.SerivceExporter;
import org.jsets.rpc.processor.Service;
import org.jsets.rpc.protocol.hessian.HessionHandler;
import org.jsets.rpc.server.NamedThreadFactory;
import org.jsets.rpc.server.NettyServer;

public class RpcTransfer {
	public static final SerivceConfig serivceConfig = SerivceConfig.insSerivceConfig();

	public static void addService(Service service) {
		SerivceExporter.addToServiceMap(service);
	}

	public static void startServer(int port, String contextRoot) {
		serivceConfig.setPort(port);
		serivceConfig.setContextRoot(contextRoot);
		
		// 导出服务
		SerivceExporter.export();
		
		ThreadFactory threadFactory = new NamedThreadFactory(serivceConfig.getContextRoot() + "-PROCESS-");
		
		ExecutorService threadPoolExecutor = new ThreadPoolExecutor(serivceConfig.getCorePoolSize(),
				                                                    serivceConfig.getMaxPoolSize(), 
				                                                    serivceConfig.getKeepAliveTime(), 
				                                                    TimeUnit.SECONDS, 
				                                                    new SynchronousQueue<Runnable>(), 
				                                                    threadFactory
				                                                    );
		HessionHandler handler = new HessionHandler(serivceConfig, threadPoolExecutor);
		NettyServer server = new NettyServer(handler, serivceConfig);
		server.start();
	}

}
